﻿//Copyright 2008 Kristian Gundry And Rune Juhl-Petersen
//This file is part of Plain.

//Plain is free software: you can redistribute it and/or modify
//it under the terms of the GNU Lesser General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//any later version.

//Plain is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU Lesser General Public License for more details.

//You should have received a copy of the GNU Lesser General Public License
//along with Plain.  If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
using Plain.Mapping;

namespace Plain.Export
{
    /// <summary>
    /// Class used to create a csv export of a list of the given type T
    /// </summary>
    /// <typeparam name="T">The type to create a csv export from</typeparam>
  /*  public class CsvExporter<T>:IExporter<T>
    {
        #region Fields

        IList<T> list = null;
        IList<string> fields=null;

        #endregion

        #region Properties

        /// <summary>
        /// Set the list of entities to create the csv from
        /// </summary>
        public IList<T> Source {
            set { list = value; }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Sets the list of fields in the csv to export
        /// </summary>
        /// <param name="fields">string list of fields</param>
        /// <returns>The exporter it self</returns>
        public IExporter<T> SetFields(IList<string> fields){
            this.fields=fields;
            return this;
        }

        /// <summary>
        /// Writes the csv export to a stream
        /// </summary>
        /// <param name="s">the stream to write to</param>
        /// <returns>the stream it self</returns>
        public Stream WriteToStream(Stream s){
                if (fields == null) {
                    fields = new List<string>();
                    foreach (PropertyInfo prop in typeof(T).GetProperties()) {
                        fields.Add(prop.Name);
                    }
                }
              
                StreamWriter writer = new StreamWriter(s);
                bool first = true;
                foreach (T entity in list) {
                    if (!first){
                        writer.WriteLine();
                    } else { first = false;}
                    bool firstProp = true;
                    foreach (string p in fields){
                        if (!firstProp)
                        {
                            writer.Write(";");
                        }
                        else {
                            firstProp = false;
                        }
                         writer.Write(PropertyUtil.GetValue<object>(entity, p).ToString());
                    
                    }

                }
                writer.Flush();
                return s;
            }          

        /// <summary>
        /// Saves the csv to a file
        /// </summary>
        /// <param name="filename">the name of the file(path)</param>
        /// <returns>True/False</returns>
        public bool Save(string filename){
            //FileStream fileStream;
            //try {
            //    fileStream = new FileStream(filename, FileMode.OpenOrCreate);
            //    this.WriteToStream(fileStream);
            //}
            //catch {
            //    return false;
            //}
            //finally{
            //    fileStream.Dispose();
            //}            
            return true;
        }

        #endregion
    }*/
}
